package com.sysmenu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;

import com.common.util.BaseAction;
import com.pojo.NewsPojo;
import com.sysmenu.service.NewsService;

public class IndustryAction extends BaseAction {
	private static final long serialVersionUID = 5729107461954468915L;
	public static Logger log = Logger.getLogger(IndustryAction.class);
	private NewsPojo newsPojo;
	private NewsService newsService;
	private String type;
	private String auditingState;
	private int page;
	private int rows;
	private int total;
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAuditingState() {
		return auditingState;
	}

	public void setAuditingState(String auditingState) {
		this.auditingState = auditingState;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	/**
	 * 行业资讯
	 * 
	 * @return
	 */
	public String addSaveIndustry() {
		newsPojo.setCreateTime(new Date());
		newsService.saveNews(newsPojo);
		return null;
	}

	public String searchNewslist() {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		if(!"-1".equals(type))
		{
			paraMap.put("type", type);
		}
		if(!"-1".equals(auditingState))
		{
			paraMap.put("auditingState", auditingState);
		}
		int start = (page - 1) * rows;
		int end = page * rows;
		paraMap.put("start", start);
		paraMap.put("end", end);
		List<NewsPojo> list = newsService.searchNewsList(paraMap);
		total=newsService.searchNewsCount(paraMap);
		StringBuffer resultMsg = new StringBuffer("{");
		resultMsg.append("\"total\":");
		resultMsg.append(total);
		String menulist = JSONArray.fromObject(list).toString();
		resultMsg.append(",\"rows\":");
		resultMsg.append(menulist);
		resultMsg.append("}");
		try {
			PrintWriter out = response.getWriter();
			response.setCharacterEncoding("utf-8");
			out.write(resultMsg.toString());

		} catch (IOException e) {
			log.error("search menulist error", e);
		}
		return null;
	}

	public NewsPojo getNewsPojo() {
		return newsPojo;
	}

	public void setNewsPojo(NewsPojo newsPojo) {
		this.newsPojo = newsPojo;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

}
